package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.Orderbill;
import cn.wolfcode.wms.domain.Orderbillitem;
import cn.wolfcode.wms.mapper.OrderbillMapper;
import cn.wolfcode.wms.mapper.OrderbillitemMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.OrderbillQueryObject;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.OrderbillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class OrderbillServiceImpl implements OrderbillService {
    @Autowired
    private OrderbillMapper orderbillMapper;
    @Autowired
    private OrderbillitemMapper orderbillitenMapper;

    public void deleteById(Long id) {
        orderbillitenMapper.deleteByBillId(id);
        orderbillMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Orderbill orderbill) {
        if (orderbill.getId() != null) {
           update(orderbill);
        } else {

                save(orderbill);

        }
    }

    private void save(Orderbill orderbill){
        //获取当前登录用户,设置录入相关信息
        //Employee currentUser = UserContext.getCurrentUser();
       // orderbill.setInputUser(currentUser);
        orderbill.setInputTime(new Date());
        //重置订单状态为未审核h
        orderbill.setStatus(orderbill.STATUS_NORMAL);
        //计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (Orderbillitem item :orderbill.getItems()){
            //计算明细金额小鸡,设置到明细对象中
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        orderbill.setTotalAmount(totalAmount);
        orderbill.setTotalNumber(totalNumber);
        //保存订单
        orderbillMapper.insert(orderbill);
        //为明细对象设置订单id,保存明细
        for (Orderbillitem item :orderbill.getItems()){
            item.setBillId(orderbill.getId());
            orderbillitenMapper.insert(item);
        }
    }
    private void update(Orderbill orderbill){
        //删除就明细
        orderbillitenMapper.deleteByBillId(orderbill.getId());
        //计算商品总金额和数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (Orderbillitem item :orderbill.getItems()){
            //为明细设置订单id
            item.setBillId(orderbill.getId());

            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //更新明细
            orderbillitenMapper.insert(item);
            //计算明细金额小鸡,设置到明细对象中
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        orderbill.setTotalAmount(totalAmount);
        orderbill.setTotalNumber(totalNumber);
        //保存订单
        orderbillMapper.updateByPrimaryKey(orderbill);
        //为明细对象设置订单id,保存明细

    }



    public Orderbill getById(Long id) {
        return orderbillMapper.selectByPrimaryKey(id);
    }

    public PageResult query(OrderbillQueryObject qo) {
        Integer totalCount = orderbillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Orderbill> data = orderbillMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    public void audit(Long id) {
        Orderbill orderbill = orderbillMapper.selectByPrimaryKey(id);
        if (orderbill.getStatus()==orderbill.STATUS_NORMAL){
           /* Employee currentUser = UserContext.getCurrentUser();
            orderbill.setAuditor(currentUser);*/
            Employee employee = new Employee();
            employee.setId(1L);
            orderbill.setAuditor(employee);
           orderbill.setAuditTime(new Date());
           orderbill.setStatus(orderbill.STATUS_AUDITED);
           orderbillMapper.updatebyauditId(orderbill);
        }
    }


}
